package com.qingzhu.util;

import com.qingzhu.pojo.CustomPage;

import java.util.ArrayList;
import java.util.List;

public class CreateCustomPage<T> {
    /* 自定义逻辑分页 */
    public CustomPage<T> createPage (List<T> list, int pageNum, int pageSize) {
        // 储存需要展示的商品
        List<T> orderVoList = new ArrayList<>();
        // 如果查询到没有商品则直接返回一个空，否则返回一个自定义的分页结果
        if (list.size() > 0) {
            // 分页逻辑
            CustomPage page = new CustomPage(pageNum,pageSize,list.size());
            int startIndex = (page.getPageNum()-1)*page.getPageSize();
            int endIndex = ((page.getPageNum()-1)*page.getPageSize() + (page.getPageSize()))-1;
            if (page.getPageNum() == page.getPages()) {
                endIndex = page.getTotal() -1;
            }
            for (int i=startIndex; i <= endIndex; i++) {
                orderVoList.add(list.get(i));
            }
            page.setList(orderVoList);
            return page;
        }else {
            return null;
        }
    }
}
